<!--
 * @Descripttion: 导出excel文件
 * @version: 
 * @Author: 淏
 * @Date: 2023-11-23 22:24:01
 * @LastEditors: 淏
 * @LastEditTime: 2024-05-05 18:44:36
-->
<template>
  <div>
    <el-button
      size="mini"
      type="success"
      class="button"
      plain
      icon="el-icon-download"
      @click="buttonSub"
      :loading="loading"
    >
      导出数据
    </el-button>
  </div>
</template>

<script>
  import { exportExcel_import } from '@/api/index.js'
  export default {
    // 组件名称
    name: 'exportExcel',
    // 组件参数 接收来自父组件的数据
    props: {
      importId: {
        type: Number,
        required: true,
      },
      importName: {
        type: String,
        required: true,
      },
      fuzzyRules: {
        type: Array,
        required: true,
      },
    },
    // 局部注册的组件
    components: {},
    // 组件状态值
    data() {
      return { loading: false }
    },
    // 计算属性
    computed: {},
    // 侦听器
    watch: {},
    // 组件方法
    methods: {
      buttonSub() {
        if (
          (this.importId != null) &
          (this.importId != NaN) &
          (this.importId != '')
        ) {
          this.getExcel()
        } else {
          this.$notify.error({
            title: '错误',
            message: '请选择数据集',
          })
        }
      },
      getExcel() {
        this.loading = true
        let importId = this.importId
        let fuzzyRules = JSON.stringify(this.fuzzyRules)
        let name = this.importName
        var qs = require('qs')
        var data = qs.stringify({
          importId: importId,
          fuzzyRules: fuzzyRules,
        })
        this.$axios
          .post('/import/exportExcel', data, {
            responseType: 'arraybuffer',
          })
          .then((res) => {
            this.$publicUtil.downloadFileBlob(
              res.data,
              name + '.xlsx',
              'application/vnd.ms-excel'
            )
            this.downloadFileBlob(
              res.data,
              this.importData.name + '数据集.xlsx'
            ) // 将接收的流传入即可
            this.loading = false
            // this.$message.success('导出成功！！')
            this.$notify({
              title: '成功',
              message: '数据导出成功！',
              type: 'success',
            })
          })
        this.loading = false
        /*      exportExcel_import(data).then((res) => {
          this.$publicUtil.downloadFileBlob(
            res,
            name + '.xlsx',
            'application/vnd.ms-excel'
          )
        }) */
      },
    },
    created() {},
    mounted() {},
  }
</script>

<style scoped>
  .button {
    background: rgba(0, 242, 255, 0.2);
    border-radius: 10px;
    border: 1px solid #295a85;
    color: #00ffff;
    text-shadow: 0 0 25px #00ffff;
    font-weight: 700;
  }
</style>
